package jdbctest.test.dao;

import jdbctest.test.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Userservcie {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<User> findAll(){
        return jdbcTemplate.query("select * from test",new Object[] {},new BeanPropertyRowMapper<>(User.class));
    };

    public void setUser(User user){
        int id=user.getId();
        String name=user.getName();
        String password=user.getPassword();
        jdbcTemplate.update("insert into test values(?,?,?)",id,name,password);
    };

    public void delete(int id){
        jdbcTemplate.update("delete from test where id=?",id);
    };


    public void updata(int id, String name,String password ){
        jdbcTemplate.update("updata test set name=?,password=? where id=?",name,password,id);
    };
}
